package fonkin.com.wifisignapp.acticity.sign;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.acticity.aboutMe.Procedure;
import fonkin.com.wifisignapp.adapter.news.ErrorAdapter;
import fonkin.com.wifisignapp.adapter.news.ScheduleAdapter;
import fonkin.com.wifisignapp.bean.schedule.Schedule;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.PrintLog;
import fonkin.com.wifisignapp.utils.ReturnVo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ErrorHistory extends AppCompatActivity {
    ListView errorHistoryListView;
    ErrorAdapter errorAdapter;
    ImageView historyDataBg;
    String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error_history);
        errorHistoryListView = (ListView)findViewById(R.id.error_listview);
        historyDataBg = (ImageView)findViewById(R.id.type_no_error_history);
        historyDataBg.setVisibility(View.GONE);
        SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(ErrorHistory.this);
        userId = preferences.getString("user_id","");
        initHistory(userId);



    }
    public void addErrorapplication(View v){
        Intent intent = new Intent(ErrorHistory.this,ErrorApply.class);
        startActivity(intent);
        finish();

    }

    public void initHistory(String userId){
        BaseRetrofit.GetBaseRetrofit().errorApplyList(userId).enqueue(new Callback<ReturnVo<List<Schedule>>>() {
            @Override
            public void onResponse(Call<ReturnVo<List<Schedule>>> call, Response<ReturnVo<List<Schedule>>> response) {
                if (response.isSuccessful()){
                    PrintLog.printApi(response,call);
                    errorAdapter = new ErrorAdapter(ErrorHistory.this, new ArrayList<Schedule>());
                    errorHistoryListView.setAdapter(errorAdapter);
                    if (response.body().getCode().equals(1)){
                        List<Schedule> data = response.body().getData();
                        if (!data.isEmpty()){
                            errorAdapter.addAll(data);
                        }else{
                            historyDataBg.setVisibility(View.VISIBLE);
                            Picasso
                                    .with(ErrorHistory.this)
                                    .load(R.drawable.none_data)
                                    .resize(500,400)
                                    .into(historyDataBg);
                        }

                    }
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<List<Schedule>>> call, Throwable t) {

            }
        });

    }
}
